<?php


namespace app\admin\model;


use think\Model;

class Article extends Model {
    protected $json = ['file'];

    public function getFileAttr($value) {
        if (empty($value)) {
            return null;
        }
        foreach ($value as &$v) {
            $v['src'] = str_replace('\\', '/', $v['src']);
        }
        return stdClassToArray($value);
    }

//    文章操作
    public function showArticle($id) {
        $article = $this->allowField(['content', 'file', 'hyperlink'])->find($id);
        return $article;
    }

    public function addArticle($data) {
        $res = $this->save($data);
        return $res;
    }

    public function editArticle($data) {
        if (empty($data['id'])) {
            return false;
        }
        $article = $this->find($data['id']);
        $res = $article->allowField(['title', 'hyperlink', 'is_jump', 'image_url', 'item_number', 'author', 'file', 'content', 'update_time'])->save($data);
        return $res;
    }

    public function getListByCategoryId($category_id, $num, $searchData) {
        if (empty($category_id)) {
            return false;
        }
        $order = [
            'update_time' => 'desc',
            'id' => 'desc',
        ];
        $where = [
            'category_id' => $category_id,
        ];
        $field = ['id', 'item_number', 'title', 'is_jump', 'hyperlink', 'clicks', 'update_time', 'status'];

        $keyword = $searchData['keyword'];
        $start = $searchData['start'];
        $end = $searchData['end'];

        if (!empty($keyword)) {
            $res = $this->
            field($field)->
            order($order)->
            where($where)->
            where('status', '<>', config('status.mysql.table_delete'))->
            whereTime('create_time', 'between', [$start, $end])->
            whereLike('title', '%' . $keyword . '%')->
            paginate([
                'list_rows' => $num,
                'query' => ['category_id' => $category_id]
            ]);
            return $res;
        } else {
            $res = $this->
            field($field)->
            order($order)->
            where($where)->
            where('status', '<>', config('status.mysql.table_delete'))->
            whereTime('create_time', 'between', [$start, $end])->
            paginate([
                'list_rows' => $num,
                'query' => ['category_id' => $category_id]
            ]);
            return $res;
        }
    }

    public function statusModById($id) {
        $article = $this
            ->where('status', '<>', config('status.mysql.table_delete'))
            ->find($id);
        $article->status = $article->status ? 0 : 1;
        $article->update_time = time();
        $res = $article->save();
        if (!$res) {
            return false;
        }
        $res = $article->status;
        return $res;
    }

    public function delById($id) {
        $article = $this->find($id);
        $article->status = config('status.mysql.table_delete');
        $res = $article->save();
        if (!$res) {
            return false;
        }
        return $res;
    }

    public function delByIds($ids) {
        $res = $this->where('id', 'in', $ids)
            ->data(['status' => config('status.mysql.table_delete')])->update();
        return $res;
    }

    public function getArticleById($id) {
        $article = $this->find($id);
        return $article;
    }

//  轮播图操作

    public function getBannerList($bannerCategoryId) {
        $order = ['id' => 'desc'];
        $field = ['id', 'item_number', 'image_url', 'title', 'is_jump', 'hyperlink', 'status', 'update_time'];
        $list = $this
            ->field($field)
            ->where(['category_id' => $bannerCategoryId])
            ->where('status', '<>', config('status.mysql.table_delete'))
            ->where('image_url', '<>', '')
            ->order($order)
            ->select();
        return $list;
    }

    public function getBannerImageById($id) {
        $imageUrl = $this
            ->field(['id', 'image_url'])
            ->find($id);
        return $imageUrl;
    }
}